package kingdee.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONObject;

import kingdee.entity.Account;
import kingdee.entity.Record;
import kingdee.entity.Users;
import kingdee.service.AccountService;
import kingdee.service.UserService;
import kingdee.service.UsersService;
import kingdee.service.impl.RecordServiceImpl;
import kingdee.util.StringUtil;
import kingdee.util.WriterUtil;
@Controller
@RequestMapping("users")
public class UsersController extends LogController{

	private int page;
	private int rows;
	private Users users;
	@Autowired
	private UsersService<Users> usersService;
	@Autowired
	private AccountService<Account> accountService;
	@Autowired
	private RecordServiceImpl recordServiceImpl;
	static Logger logger = Logger.getLogger(UsersController.class);
	
	
	@RequestMapping("usersIndex")
	public String index(){
		return "users";
	}
	
	
	@RequestMapping("usersList")
	public void userList(HttpServletRequest request,HttpServletResponse response){
		try {
			page = Integer.parseInt(request.getParameter("page"));
			rows = Integer.parseInt(request.getParameter("rows"));
			users = new Users();
			users.setPage((page-1)*rows);
			users.setRows(rows);
			users.setUser_name(request.getParameter("user_name"));
		//	String roleId = request.getParameter("roleId");
			/*if (StringUtil.isNotEmpty(roleId)) {
				user.setRoleId(Integer.parseInt(roleId));
			} else {
				user.setRoleId(null);
			}*/
			List<Users> list= usersService.findUsers(users);
			int total = usersService.countUsers(users);
			JSONObject jsonObj = new JSONObject();
			jsonObj.put("total",total );
			jsonObj.put("rows", list);
	        WriterUtil.write(response,jsonObj.toString());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("用户展示错误",e);
		}
	}
	
	
	// 新增或修改
	@RequestMapping("reserveUsers")
	public void reserveUser(HttpServletRequest request,Users users,HttpServletResponse response){
		String user_id = request.getParameter("user_id");
		JSONObject result=new JSONObject();
		try {
			if (StringUtil.isNotEmpty(user_id)) {   // userId不为空 说明是修改
				users.setUser_id(user_id);
				usersService.updateUsers(users);
				result.put("success", true);
			}/*else {   // 添加
				if(usersService.existUserWithUserName(users.getUserName())==null){  // 没有重复可以添加
					usersService.addUser(users);
					result.put("success", true);
				} else {
					result.put("success", true);
					result.put("errorMsg", "该用户名被使用");
				}
			}*/
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("修改用户信息错误",e);
			result.put("success", true);
			result.put("errorMsg", "对不起，操作失败");
		}
		WriterUtil.write(response, result.toString());
	}
	
	
	@RequestMapping("deleteUsers")
	public void delUser(HttpServletRequest request,HttpServletResponse response){
		JSONObject result=new JSONObject();
		try {
			String[] ids=request.getParameter("ids").split(",");
			for (String id : ids) {
				usersService.deleteUsers(id);
			}
			result.put("success", true);
			result.put("delNums", ids.length);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除用户信息错误",e);
			result.put("errorMsg", "对不起，删除失败");
		}
		WriterUtil.write(response, result.toString());
	}
	
	@RequestMapping("useraccountinfo")
	public  void getUserAccountInfo(HttpServletRequest request,HttpServletResponse response){
		JSONObject result=new JSONObject();
		try{
			String user_id = request.getParameter("userId");
			
			if(StringUtil.isNotEmpty(user_id));
			
			
			//找用户账户
		    List<Account> accountLists=accountService.findAccount(user_id);
		    List<Record> recordLists=new ArrayList<>();
		    List<Record> recordlists=new ArrayList<>();
			for(int i=0;i<accountLists.size();i++){
				//找到账户下的每一个账户记录
				Long  account_id=accountLists.get(i).getId();
				recordlists=recordServiceImpl.findRecord(user_id,account_id);
				recordLists.addAll(recordlists);
			}
			result.put("success", true);
			result.put("recordLists", recordLists);
		}
		catch(Exception e) {
			e.printStackTrace();
			logger.error("请求失败",e);
			result.put("errorMsg", "对不起，请求失败");
		}
		WriterUtil.write(response, result.toString());
	}
}
